﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HodhodNews;
using Googler.DAL;
using HodhodNews.Aggregation;
using HodhodNews.Storage;
using Googler.BLL;
using HodhodNews.Controllers;

namespace Googler.Web
{
    public partial class _Default : System.Web.UI.Page
    {
        List<News> _NewsList;
        public List<News> NewsList
        {
            get
            {
                return _NewsList;
            }
            set
            {
                _NewsList = value;
            }
        }
        

        //GoogleNewsAggregationProvider serivce;
        //public GoogleNewsAggregationProvider NewsService { get { serivce = serivce ?? new GoogleNewsAggregationProvider(Keywords); return serivce; } }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtFrom.Text = DateTime.Now.AddDays(-1).ToShortDateString();
                txtTo.Text = DateTime.Now.ToShortDateString();

                DateTime from = Convert.ToDateTime(txtFrom.Text);
                DateTime to = Convert.ToDateTime(txtTo.Text);

                NewsList = StorageProvider.Insance.GetNews(from, to);
                DataBindUI();
            }
        }

        protected void btnDownload_Click(object sender, EventArgs e)
        {
            NewsList = NewsAggregationProvider.Instance.Aggregate(new AggregationOptions()
            {
                Keywords = new UserController().GetKeywords()
            });

            StorageProvider.Insance.Save(NewsList);
            RefreshNews();
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshNews();
        }

        private void RefreshNews()
        {
            DateTime from = Convert.ToDateTime(txtFrom.Text);
            DateTime to = Convert.ToDateTime(txtTo.Text);

            NewsList = StorageProvider.Insance.GetNews(from, to);
            renderNewsList();
        }

        private void renderNewsList()
        {
            if (NewsList == null || NewsList.Count == 0)
            {
                lblNewsCount.Text = "0";
                return;
            }

            DataBindUI();
        }

        string DocumentText;
        const string html = "<HTML dir=rtl ><body>{0}</body></HTML>";
        public virtual void DataBindUI()
        {

            if (NewsList == null || NewsList.Count == 0)
            {
                this.DocumentText = "";
                return;
            }

            string content = "";

            foreach (News atom in NewsList)
            {
                content += atom.RenderSummeries();
            }
            this.DocumentText = string.Format(html, content);
            Label1.Text = this.DocumentText;
            lblNewsCount.Text = "عدد الأخبار " + NewsList.Count.ToString();
        }
    }
}
